<template>
    <div>
        <Table highlight-row ref='table' :columns='columns' :loading='loading' :data='data' :height="520">
            <template slot-scope="{row}" slot="info">
                <div>  <Avatar shape="square" size="large" class="ivu-fl" :src="row.avatar" ></Avatar>
                    <span class="ivu-ml-8">{{ row.nickname }}</span>
                    <br >
                <span class="ivu-ml-8 ivu-weixin">微信号：{{ row.friend_wechatno }}</span></div>
            </template>
            <template slot-scope="{ row }" slot="source_type">
                <span v-if="+row.source_type<10000 && +row.source_type !== 0">主动</span>
                <span v-if="+row.source_type>10000">被动</span>
                <span v-if="row.source_type===null">{{'/'}}</span>
            </template>
            <template slot-scope="{row}" slot="option">
                <a @click="openedits(row)">详细</a>
            </template>
        </Table>

       <Page
            show-total
            show-elevator
            show-sizer
            :total="total"
            :current.sync="params.page"
            :page-size="params.pageSize"
            :page-size-opts="pageOpts"
            @on-change="changepage"
            @on-page-size-change="changepageSize"
            class="ivu-mt-16"
        ></Page>
        <Drawer v-model="contactModal" width="960" @on-close="closedrawer">
            <div v-if="isshows" style="margin-top: 30px;">
                <div style="
                                display: flex;
                                align-items: center;
                                width: 100%;
                                justify-content: space-between;
                            ">
                    <div style="display: flex; align-items: center;" v-for="(item, index) in datacontent"
                         :key="index">
                        <div>
                            <img class="headerImgs" :src="datacontent[0].avatar" alt="" >
                        </div>
                        <div style="margin-left: 10px;">
                            <div style="
                                            font-size: 16px;
                                            font-weight: 900;
                                        ">
                                {{ datacontent[0].nickname }}
                            </div>
                            <div>
                                <span>
                                    微信号：
                                    {{
                                        datacontent[0].friend_wechatno ||
                                            datacontent[0].friendid
                                    }}
                                </span>
                                <span style="margin-left: 20px;">
                                    地区：
                                    {{
                                        datacontent[0].province +
                                            datacontent[0].city
                                    }}
                                </span>
                            </div>
                        </div>
                    </div>
                    <div v-if="currentBtnShow !== '8'">
                        <ButtonGroup>
                            <template v-for="(item,index) in clueList">
                                <Button v-if="+item.value !== 7 && +item.value !== 8 &&+item.value !== 9" :key="index" @click="handleContact(item.value)">{{ item.label }}</Button>
                            </template>
                        </ButtonGroup>
                        <template  v-for="item in clueList">
                            <Button style="margin-left: 30px;" type="primary" v-if="+item.value === 8" :key="item.value" @click="handleContact(item.value)">{{item.label }}</Button>
                        </template>
                    </div>
                </div>
            </div>
            <Row :gutter="32">
                <Col span="8">
                <div style="margin-top: 18px;">
                    <Select v-model="currentWx" @on-change="changeWx">
                        <Option :value="item.value" v-for="(item,index) in otherWxList" :key="index">{{
                            item.label
                        }}</Option>
                    </Select>
                </div>
                <div class="ivu-mt-16" v-if="dataparamslist&&dataparamslist.tags_arr&&dataparamslist.tags_arr.length">
                    <tag v-for="(item,
                                 index) in dataparamslist.tags_arr" :key="index">{{ item }}</tag>
                </div>
                <DescriptionList class="ivu-mt-16" :col="1" v-if="dataparamslist">
                    <Description term="来源：">{{
                        dataparamslist.source_type_str
                    }}</Description>
                    <Description term="备注名：">{{
                        dataparamslist.remark_name
                    }}</Description>
                    <Description term="微信状态：">
                        <Badge status="success" v-if="+dataparamslist.state === 0"
                               :text="dataparamslist.state_str" ></Badge>
                        <Badge status="warning" v-else-if="+dataparamslist.state === 1"
                               :text="dataparamslist.state_str" ></Badge>
                        <Badge status="error" v-else-if="+dataparamslist.state === 2"
                               :text="dataparamslist.state_str" ></Badge>
                        <Badge status="warning" v-else-if="+dataparamslist.state === 3"
                               :text="dataparamslist.state_str" ></Badge>
                        <Badge status="error" v-else-if="+dataparamslist.state === 4"
                               :text="dataparamslist.state_str" ></Badge>
                    </Description>
                    <Description term="加好友天数：">{{
                        dataparamslist.create_day
                    }}</Description>
                    <Description term="未互动天数：">{{
                        dataparamslist.unchat_day
                    }}</Description>
                    <Description term="通过时间：">{{
                        dataparamslist.create_time
                    }}</Description>
                </DescriptionList>
                <Divider class="ivu-mt-8" ></Divider>
                <Timeline style="max-height: 400px; overflow: auto;">
                    <TimelineItem v-for="item in contactlist" :key="item.id">
                        <p class="time">{{ item.created_at }}</p>
                        <p class="content">{{ item.remark }}</p>
                        <p class="consultant">
                            <span class="ivu-mr-8">{{
                                item.realname
                            }}</span>
                            <Badge status="processing" :text="item.status_name" ></Badge>
                        </p>
                    </TimelineItem>
                </Timeline>
                        </Col>
                <Col span="16">
                <div class="sky-record ivu-mt-16">
                    <chatDetail :paramsrecord="paramsrecord" ref="liaotian" ></chatDetail>
                </div>
                </Col>
            </Row>
        </Drawer>
        <Modal title="处理信息" v-model="showedit" width="700" @on-cancel="canceledits('formDynamic')">
            <Form ref="formDynamic" :model="paramsedits" label-position="top" :rules="createRuless">
                <template v-if="!contactDatas">
                    <FormItem label="备注:">
                        <Input placeholder="请备注原因" v-model="paramsedits.remark" ></Input>
                    </FormItem>
                </template>
                <template v-if="contactDatas">
                    <FormItem :label-width="20">
                        <RadioGroup v-model="paramsedits.patient_user_id" @on-change="changeLabel">
                            <Radio label="1">新客户</Radio>
                            <Radio label="2">老客户</Radio>
                        </RadioGroup>
                    </FormItem>
                    <template v-if="paramsedits.patient_user_id === '2'">
                        <FormItem :label-width="20" prop="patient_user_id_1" :rules="oldNameRules">
                            <Select style="width: 300px;" clearable v-model="paramsedits.patient_user_id_1"
                                    placeholder="请输入老患者姓名" filterable remote clearable :remote-method="remoteMethod"
                                    :loading="loadingcity" @on-change="changOld">
                                <Avatar :src="
                                    require('../../assets/images/wx.png')
                                " slot="prefix" size="small" v-show="showIcon" ></Avatar>
                                <Option :value="item.value" v-for="item in oldPatient" :key="item.value">{{
                                item.label }}</Option>
                            </Select>
                        </FormItem>
                    </template>
                    <template v-if="paramsedits.patient_user_id === '1'">
                        <Row :gutter="24">
                            <Col span="5">
                            <FormItem label="姓名">
                                <Input v-model="paramsedits.userName" placeholder="请输入姓名" ></Input>
                            </FormItem>
                                    </Col>
                            <Col span="6">
                            <FormItem label="性别">
                                <RadioGroup v-model="paramsedits.sex">
                                    <Radio :label="0">未知</Radio>
                                    <Radio :label="1">男</Radio>
                                    <Radio :label="2">女</Radio>
                                </RadioGroup>
                            </FormItem>
                                    </Col>
                            <Col span="4">
                            <FormItem label="年龄" prop="age">
                                <InputNumber :max="100" :min="1" v-model="paramsedits.age"></InputNumber>
                            </FormItem>
                                    </Col>
                            <Col span="9">
                            <FormItem label="城市" required>
                                <!-- <Select ref="citySearch" v-model="paramsedits.city" placeholder="请输入城市拼音简写"
                                            filterable remote clearable :remote-method="remoteMethods"
                                            :loading="loadingcity">
                                            <Option v-for="item in optionscity" :value="item.value" :key="item.value">{{
                                                item.label }}</Option>
                                        </Select> -->
                                <jgcheckcity ref="checkcity" @cityvalue="cityvalue" v-model="city_id" :name="`wechat`"></jgcheckcity>
                            </FormItem>
                                    </Col>
                        </Row>
                        <Row :gutter="24">
                            <Col span="8">
                            <FormItem label="手机号" prop="mobile">
                                <Input v-model="paramsedits.mobile"
                                       @on-keyup="paramsedits.mobile=paramsedits.mobile.replace(/\s+|[\r\n]/g,'')"
                                       @on-blur="
                                           telrepeat(
                                               paramsedits.mobile
                                           )
                                       " placeholder="请输入手机号" ></Input>
                                <div style="color:#ff9900;font-size:18px" v-if="patientStatus">！老患者到诊</div>
                            </FormItem>
                                    </Col>
                            <Col span="8">
                            <FormItem label="微信号">
                                <Input v-model="paramsedits.weChartNum"
                                       @on-keyup="paramsedits.weChartNum=paramsedits.weChartNum.replace(/\s+|[\r\n]/g,'')"
                                       @on-blur="
                                           WXrepeat(
                                               paramsedits.weChartNum
                                           )
                                       " placeholder="请输入微信号" ></Input>
                            </FormItem>
                                    </Col>
                        </Row>
                        <Row :gutter="24">
                            <Col span="8">
                            <FormItem label="一级病种" prop="oneDiseases">
                                <Select placeholder="请选择一级病种" v-model="
                                    paramsedits.oneDiseases
                                " clearable @on-clear="clearOne" @on-change="changeOne">
                                    <Option v-for="item in diseasesArr1" :value="item.value" :key="item.value">
                                        {{ item.label }}</Option>
                                </Select>
                            </FormItem>
                                    </Col>
                            <Col span="8">
                            <FormItem label="二级病种" :rules="twoRules" prop="twoDiseases"
                                      v-show="diseasesArr2.length>0">
                                <Select placeholder="请选择二级病种" v-model="
                                    paramsedits.twoDiseases
                                " clearable>
                                    <Option v-for="item in diseasesArr2" :value="item.value" :key="item.value">
                                        {{ item.label }}</Option>
                                </Select>
                            </FormItem>
                                    </Col>
                            <Col span="8">
                            <FormItem label="患者状态" prop="patientStatus">
                                <Select placeholder="请选择" v-model="
                                    paramsedits.patientStatus
                                " clearable>
                                    <Option v-for="item in patientStatusArr" :value="item.value" :disabled="
                                        +item.is_select === 0
                                    " :key="item.value">{{ item.label }}</Option>
                                </Select>
                            </FormItem>
                                    </Col>
                        </Row>
                        <Row>
                            <Col span="12">
                            <FormItem label="部位" prop="bodyPart" :rules="bodyPartRule">
                                <TreeSelect :max-tag-count="2" v-model="paramsedits.bodyPart" :data="postList"
                                            clearable multiple ></TreeSelect>
                            </FormItem>
                                    </Col>
                        </Row>
                        <template v-if="
                            paramsedits.patientStatus === '2' ||
                                paramsedits.patientStatus === '1'
                        ">
                            <FormItem label="下次回访时间" prop="day">
                                <Row :gutter="16" type="flex">
                                    <Col>
                                    <InputNumber :formatter="$inputday" :step="1" :min="0" v-model.number="
                                        paramsedits.day
                                    " @on-change="
                                        blurday(paramsedits.day)
                                    ">
                                    </InputNumber>
                                            </Col>
                                    <Col>天后</Col>
                                    <Col>
                                    <DatePicker :editable="false" :options="optionss" type="date"
                                                v-model="DatePicker" @on-change="
                                                    changeday(DatePicker)
                                                " placeholder="请输入回访时间">
                                    </DatePicker>
                                            </Col>
                                </Row>
                            </FormItem>
                        </template>
                        <template v-if="paramsedits.patientStatus === '1'">
                            <FormItem label="预到时间" prop="day1">
                                <Row :gutter="16" type="flex">
                                    <Col>
                                    <InputNumber :step="1" :min="0" v-model.number="
                                        paramsedits.day1
                                    " @on-change="
                                        blurday1(
                                            paramsedits.day1
                                        )
                                    ">
                                    </InputNumber>
                                            </Col>
                                    <Col>天后</Col>
                                    <Col>
                                    <DatePicker :editable="false" :options="optionss" type="date"
                                                v-model="DatePicker1" @on-change="
                                                    changeday1(DatePicker1)
                                                " placeholder="请输入预约时间">
                                    </DatePicker>
                                            </Col>
                                </Row>
                            </FormItem>
                        </template>
                        <FormItem label="备注" prop="remark">
                            <Input type="textarea" placeholder="请备注原因" v-model="paramsedits.remark"
                                   :autosize="{ minRows: 2, maxRows: 8 }" ></Input>
                        </FormItem>
                    </template>
                </template>
            </Form>
            <div slot="footer">
                <Button @click="canceledits('formDynamic')">取消</Button>
                <Button type="primary" :loading="remarksloading"
                        @click="handlesearch('formDynamic')">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import chatDetail from './../../pages/robot/wechatFriend/chatrecord.vue';
// import jgcheckcity from '@/components/checkcity';
import {
        wechatusercluelist,
        wechatuserclueinfo,
        clueusercontact,
        concatWxPatient,
        wechatmessagelist,
        getOldPatient,
        patientindexsearchcity,
        patientindexsearchmobile,
        repeatbaiyou
    } from '@api/hqapi';
import {searchClue} from '@/api/globalsearch.js';
let lodash = require('lodash');
export default {
    components: { chatDetail},
    data(){
        const validateMobile = (rule, value, callback) => {
                if(value === '') {
                    callback();
                }else {
                    let reg = /^\d{10,12}$/;
                    if(reg.test(value)) {
                        callback();
                    }else {
                        callback(new Error('请输入正确的联系号码'));
                    }
                }
            };
        return{
            columns: [
                {
                    title: '微信信息',
                    slot: 'info',
                    minWidth: 280,
                },
                {
                    title: '微信备注',
                    key: 'remark',
                    minWidth: 100,
                    ellipsis: true,
                    tooltip: true,
                    render: (h,params) => {
                        return h('Ellipsis',{props:{'text':params.row.remark?params.row.remark:'/' ,'length':6,'tooltip':true}}); 
                    }
                },
                {
                    title: '最近沟通时间',
                    key: 'last_communication_time',
                    minWidth: 140,
                    render: (h,params) => {
                        return h('span',params.row.last_communication_time ? params.row.last_communication_time : '/'); 
                    }
                },
                {
                    title: '客服微信号',
                    key: 'wechatno',
                    minWidth: 140,
                    ellipsis: true,
                    tooltip: true,
                    render: (h,params) => {
                        return h('span',params.row.wechatno ? params.row.wechatno : '/'); 
                    }
                },
                {
                    title: '添加方式',
                    slot: 'source_type',
                    minWidth: 100,
                    ellipsis: true,
                    tooltip: true,
                },
                {
                    title: '归属部门',
                    key: 'department_name',
                    minWidth: 100,
                    render: (h,params) => {
                        return h('span',params.row.department_name ? params.row.department_name : '/'); 
                    }
                },
                {
                    title: '联系人员',
                    key: 'operation_name',
                    minWidth: 100,
                    render: (h,params) => {
                        return h('span',params.row.operation_name ? params.row.operation_name : '/'); 
                    }
                },
                {
                    title: '添加时间',
                    key: 'created_at',
                    minWidth: 180,
                    render: (h,params) => {
                        return h('span',params.row.created_at ? params.row.created_at : '/'); 
                    }
                },
                {
                    title: '操作',
                    slot: 'option',
                    fixed: 'right',
                    minWidth: 80,
                },
            ],
            loading: false,
            data: [{
                index: 1
            }],
            contactModal: false,
            datacontent: [],
            currentWx: '',
            dataparamslist: {},
            optionscity: [],
            patientStatusArr: [],
            diseasesArr2: [],
            contactlist: [],
            diseasesArr1: [],
            showIcon: false,
            clueList: [],
            friend_wechatno: '',
            isshows: true,
            otherWxList: [],
            paramsrecord: { wechat_id: '', friend_id: '' },
            currentBtnShow: '1',
            showedit: false,
            paramsedits: {
                id: undefined,
                clue_status_id: '',
                remark: '',
                day: 1,
                bodyPart: [],
                day1: 0,
                type: 3,
                patient_user_id: '1',
                weChartNum: '',
                mobile: '',
                patient_user_id_1: '',
                userName: '',
                age: null,
                birthday: '',
                sex: '',
                city_id: '',
                oneDiseases: '',
                twoDiseases: '',
                patientStatus: ''
            },
            patientStatus: false, // 是否老患者到诊状态
            contactDatas: true,
            remarksloading: false,
            city_id: [],
            postList: [],
            DatePicker: new Date(new Date().getTime() + 86400000),
            DatePicker1: new Date(new Date().getTime()),
            preBirthday: '',
            createRuless: {
                    mobile: [{ validator: validateMobile, trigger: 'blur' }],

                    city_id: [
                        {
                            required: true,
                            message: '请选择城市',
                            trigger: 'change'
                        }
                    ],
                    age: [
                        {
                            required: true,
                            message: '请填写年龄',
                            type: 'number',
                            trigger: 'blur,change'
                        }
                    ],
                    remark: [
                        {
                            required: false
                        }
                    ],
                    oneDiseases: [
                        {
                            required: true,
                            message: '请选择一级病种',
                            trigger: 'change'
                        }
                    ],
                    patientStatus: [
                        {
                            required: true,
                            message: '请选择患者状态',
                            trigger: 'change'
                        }
                    ],
                    // patient_user_id_1: [
                    //     {
                    //         required: true,
                    //         message: "请选择老患者",
                    //         trigger: ["change",'blur'],
                    //     },
                    // ],
                    day: [
                        {
                            required: true,
                            type: 'number',
                            message: '请输入回访时间',
                            trigger: 'change'
                        }
                    ],
                    day1: [
                        {
                            required: true,
                            type: 'number',
                            message: '请输入预约',
                            trigger: 'change'
                        }
                    ]
                },
            optionss: {
                disabledDate(date) {
                    return (
                        date && date.valueOf() < Date.now() - 24 * 3600 * 1000
                    );
                }
            },
            total:0,
            pageOpts: [10, 15, 20, 30, 50],
            params:{
                page: 1,
                pageSize: 10,
            },
            keyword:''
        };
    },
    computed: {
        bodyPartRule() {
            if(this.paramsedits.oneDiseases === '1') {
                return {
                    required: true,
                    validator: (rule, value, callback) => {
                        if(value.length) {
                            callback();
                        }else {
                            callback(new Error('请选择部位'));
                        }
                    },
                    trigger: 'change'
                };
            }else {
                return {
                    required: false,
                    type: 'array',
                    trigger: 'change'
                };
            }
        },
        twoRules() {
            if(this.diseasesArr2.length) {
                return {
                    required: true,
                    message: '请选择二级病种',
                    trigger: 'change'
                };
            }else {
                return {
                    required: false
                };
            }
        },
        dayRules() {
            if(this.paramsedits.patient_user_id === '1') {
                return {
                    required: true,
                    type: 'number',
                    message: '请输入下次回访时间',
                    trigger: 'blur'
                };
            }else {
                return {
                    required: false
                };
            }
        },
        birthdays() {
            if(this.paramsedits.age !== '') {
                var year = new Date().getFullYear() - this.paramsedits.age;
                return year + '-01' + '-01';
            }
        },
        oldNameRules() {
            if(this.paramsedits.patient_user_id === '2') {
                return {
                    required: true,
                    message: '请选择老患者姓名',
                    trigger: 'change'
                };
            }else {
                return {
                    required: false
                };
            }
        }
    },
    methods: {
        initial(val){
            this.funaskData = wechatusercluelist;
            this.funupdate = concatWxPatient;
            this.keyword=val
            this.getsearchClue(val);
        },
        changepage(e) {
            this.params.page = e;
            this.getsearchClue(this.keyword);
        },
        /* @函数功能: 每页数量 */
        changepageSize(e) {
            this.params.pageSize = e;
            this.getsearchClue(this.keyword);
        },
        getsearchClue(val){
            this.loading=true
            searchClue({type: 'wechat',true_keyword: val,...this.params}).then(res => {
                this.total = res.total;
                this.data = res.list.list;
                this.clueList = res.clue_status_id;
                this.loading=false
            }).catch(()=>{
                this.loading=false
            });
        },
        handleContact(state) {
            console.log(state,'state');
            this.paramsedits.clue_status_id = state;
            this.showedit = true;
            // this.openedit(state);
            for(let item of this.clueList) {
                if(state === item.value) {
                    if(+item.value === 8) {
                        // this.paramsedits.patient_user_id = "1";
                        this.contactDatas = true;
                    }else {
                        this.contactDatas = false;
                    }
                }
            }
        },
        cityvalue(city) {
            this.city_id = city;
            this.paramsedits.city_id = city.join(',');

            // this.askData();
        },
        changeWx(value) {
            if(value) {
                this.paramsrecord.wechat_id = value;
                this.$nextTick(() => {
                    console.log('4444');
                    this.$refs.liaotian.askDatas('end');
                });
            }
        },
        openedits(row) {
            this.currentBtnShow = row.clue_status;
            this.paramsedits.id = row.id;
            this.paramsedits.weChartNum = row.friend_wechatno;
            this.friend_wechatno = row.friend_wechatno;
            this.axiosWxInfo(this.friend_wechatno, this.paramsedits.id);
        },
        axiosWxInfo(friend_wechatno, id, value) {
            wechatuserclueinfo({
                friend_wechatno,
                id
            }).then(res => {
                this.datacontent = res.list;
                if(res.list.length > 0) {
                    this.currentWx = this.currentWx ? this.currentWx : res.list[0].wechat_list[0] ? res.list[0].wechat_list[0].wechatid : '';
                }
                if(
                    res.default_city &&
                    JSON.stringify(res.default_city) !== '{}'
                ) {
                    this.optionscity = [
                        {
                            value: res.default_city.city_id,
                            label: res.default_city.city_name
                        }
                    ];
                }
                this.postList = res.body_parts_list;
                this.contactlist = res.contact;
                this.diseasesArr1 = res.diseases;
                this.patientStatusArr = res.patient_status;
                this.paramsedits.patientStatus = '2';
                if(this.currentWx) {
                    this.contactModal = true;
                    this.dataparams(res.list[0].id);
                }else {
                    this.$Message.error('未绑定客服微信');
                }
            });
        },
        dataparams(id) {
            this.dataid = id;
            this.datacontent.forEach(item => {
                if(item.id === id) {
                    this.dataparamslist = item.wechat_list[0];
                    this.wechatmessagelists(item);
                }
            });
        },
        wechatmessagelists(row) {
            this.otherWxList = row.wechat_list.map(ee => {
                return {
                    ...ee,
                    value: ee.wechatid,
                    label: ee.wechatnick,
                    friend_wechatno: ee.wechatno
                };
            });

            this.paramsrecord.wechat_id = row.wechat_list[0] ? row.wechat_list[0].wechatid : '';
            this.paramsrecord.friend_id = row.friendid;
            this.$nextTick(() => {
                this.$refs.liaotian.askDatas('end');
            });
        },
        closedrawer() {
            this.$refs.formDynamic.resetFields();
            this.paramsedits = {
                id: undefined,
                clue_status_id: '',
                remark: '',
                day: 1,
                weChartNum: '',
                mobile: '',
                day1: 0,
                bodyPart: [],
                type: 3,
                patient_user_id: '1',
                patient_user_id_1: '',
                userName: '',
                age: null,
                birthday: '',
                sex: '',
                city_id: '',
                oneDiseases: '',
                twoDiseases: '',
                patientStatus: ''
            };
            this.currentWx = '';
            this.diseasesArr1 = [];
            this.diseasesArr2 = [];
            this.patientStatus = false;
            // this.askData();
        },
        clearOne() {
            this.diseasesArr2 = [];
            this.paramsedits.twoDiseases = '';
        },
        changeOne(value) {
            if(value) {
                this.paramsedits.twoDiseases = '';
                this.diseasesArr2 = this.diseasesArr1.filter(
                    ee => {
                        return ee.value == value; 
                    }
                )[0].children;
                if(!this.diseasesArr2.length) {
                    this.$nextTick(() => {
                        this.$refs.formDynamic.validateField('twoDiseases');
                    });
                }
            }
        },
        changeLabel(value) {
            this.$refs.formDynamic.resetFields();
        },
        /* @函数功能: 取消编辑或新增 */

        canceledits(name) {
            // if (name) this.$refs[name].resetFields();

            this.city_id = [];
            this.showedit = false;
            // this.paramscancel();
            },
            /* @函数功能: 提交编辑或新增 */

        // 搜索手机号
        handlesearch(name) {
            this.$refs[name].validate(valid => {
                if(valid) {
                    this.city_id = [];
                    if(this.paramsedits.clue_status_id === '8') {
                        this.remarksloading = true;
                        let config = {
                            id:
                                this.paramsedits.id === undefined
                                    ? 0
                                    : this.paramsedits.id,
                            mobile: this.paramsedits.mobile,
                            wechat_id: this.paramsedits.weChartNum
                        };
                        patientindexsearchmobile(config).then(res => {
                            if(+res.result === 1) {
                                this.$Modal.confirm({
                                    title: '提示',
                                    content: '<p>该手机号码已存在，是否继续录入?</p>',
                                    onOk: () => {
                                        this.submitedits(name);
                                    },
                                    onCancel: () => {
                                        this.remarksloading = false;
                                    }
                                });
                            }else if(+res.result === 2) {
                                this.$Modal.confirm({
                                    title: '提示',
                                    content: '<p>该微信号已存在，是否继续录入?</p>',
                                    onOk: () => {
                                        this.submitedits(name);
                                    },
                                    onCancel: () => {
                                        this.remarksloading = false;
                                    }
                                });
                            }else if(+res.result === 3) {
                                this.$Modal.confirm({
                                    title: '提示',
                                    content: '<p>该手机号/微信号已存在，是否继续录入?</p>',
                                    onOk: () => {
                                        this.submitedits(name);
                                    },
                                    onCancel: () => {
                                        this.remarksloading = false;
                                    }
                                });
                            }else {
                                this.submitedits(name);
                            }
                        });
                    }else {
                        this.submitedits(name);
                    }
                }
            });
        },
        submitedits(name) {
            if(name) {
                this.$refs[name].validate(valid => {
                    if(valid) {
                        this.submiteditss();
                    }
                });
            }else {
                this.submiteditss();
            }
        },
        submiteditss() {
            let config = this.paramssubmit();
            console.log(config,'config1');
            if(config) {
                this.remarksloading = true;
                console.log(this.contactDatas,'111');
                if(this.contactDatas) {
                    this.funupdate(config).then(res => {
                        this.remarksloading = false;
                        if(+res.clue_status_id === 8) {
                            this.contactModal = false;
                            this.showedit = false;
                            // this.askData();
                            this.diseasesArr2 = [];
                            this.paramsedits = {
                                id: undefined,
                                clue_status_id: '',
                                remark: '',
                                day: 1,
                                day1: 0,
                                weChartNum: '',
                                mobile: '',
                                type: 3,
                                bodyPart: [],
                                patient_user_id: '1',
                                patient_user_id_1: '',
                                userName: '',
                                age: null,
                                birthday: '',
                                sex: '',
                                city_id: '',
                                oneDiseases: '',
                                twoDiseases: '',
                                patientStatus: ''
                            };
                            this.$refs.formDynamic.resetFields();
                        }else {
                            this.beforsubmits();
                        }
                    });
                }else {
                    this.funcreate(config).then(() => {
                        this.remarksloading = false;
                        this.beforsubmits();
                    });
                }
                
            }
        },
        /* @函数功能: 提交之后数据处理 */
        beforsubmits() {
            this.$Message.success('处理成功');
            // this.paramscancel();
            // 打开抽屉需带的参数
            // this.openedits({
            //     id: this.paramsedits.id,
            //     friend_wechatno: this.friend_wechatno
            // });
            this.city_id = [];
            this.showedit = false;
            this.contactModal = false;
            // this.askData();
        },
        telrepeat(data) {
            let config = {
                id: this.paramsedits.id === undefined ? 0 : this.paramsedits.id,
                mobile: data
            };
            patientindexsearchmobile(config).then(res => {
                if(+res.result === 1) {
                    this.$Message.error({
                        duration: 2.5,
                        content: '该手机号码已存在'
                    });
                }
            });
            repeatbaiyou({mobile: data}).then(res => {
                // console.log(res,'res')
                this.patientStatus = res;
            });
        },
        WXrepeat(data) {
            let config = {
                id: this.paramsedits.id === undefined ? 0 : this.paramsedits.id,
                wechat_id: data
            };

            patientindexsearchmobile(config).then(res => {
                if(+res.result === 2) {
                    this.$Message.error({
                        duration: 2.5,
                        content: '该微信号已存在'
                    });
                }
            });
        },
        paramssubmit() {
            let config = {
                id: this.paramsedits.id,
                clue_status_id: this.paramsedits.clue_status_id,
                day: parseInt(new Date(this.DatePicker).getTime() / 1000),
                type: 3,
                patient_status_id:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.patientStatus
                        : '',
                user_name:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.userName
                        : '',
                age:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.age
                        : '',
                // birthday:
                //     this.paramsedits.clue_status_id === "8"
                //         ? this.paramsedits.age === this.preBirthday
                //             ? this.paramsedits.birthday
                //                 ? this.paramsedits.birthday
                //                 : this.birthdays
                //             : this.birthdays
                //         : "",
                birthday:
                    this.paramsedits.clue_status_id === '8'
                        ? this.birthdays
                        : '',
                patient_diseases_id:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.oneDiseases
                        : '',
                patient_diseases_child_id:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.twoDiseases
                        : '',
                patient_remake:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.remark
                        : '',
                city_id: this.paramsedits.clue_status_id === '8' ? this.paramsedits.city_id : '',

                appointment_day: parseInt(
                    new Date(this.DatePicker1).getTime() / 1000
                ),
                gender:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.sex
                        : '',
                // city_name: this.paramsedits.city
                //     ? this.optionscity.filter(
                //         (ee) => ee.value === this.paramsedits.city
                //     )[0].label
                //     : "",
                patient_user_id:
                    this.paramsedits.patient_user_id === '2'
                        ? this.paramsedits.patient_user_id_1
                        : '',
                friend_wechatno:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.weChartNum
                        : '',
                mobile:
                    this.paramsedits.clue_status_id === '8'
                        ? this.paramsedits.mobile
                        : '',
                body_parts_id: this.paramsedits.clue_status_id === '8'
                    ? this.paramsedits.bodyPart.join(',')
                    : ''
            };
            let config1 = {
                id: this.paramsedits.id,
                clue_status_id: this.paramsedits.clue_status_id,
                remark: this.paramsedits.remark,
                day: 1,
                type: 3
            };
            return this.paramsedits.clue_status_id === '8' ? config : config1;
        },
        blurday(num) {
            this.DatePicker = this.$blurday(num);
        },
        changeday(date) {
            this.paramsedits.day = this.$changeday(date);
        },
        blurday1(num) {
            this.DatePicker1 = this.$blurday(num);
        },
        changeday1(date) {
            this.paramsedits.day1 = this.$changeday(date);
        },
        getDatas: lodash.debounce(function(params) {
            if(this.oldPatient.every(ee => {
                return ee.label !== params; 
            })) {
                this.loadingcity = true;
                getOldPatient({
                    user_name: params
                }).then(res => {
                    this.$nextTick(() => {
                        this.loadingcity = false;
                        this.oldPatient = Object.freeze(
                            res.map(ee => {
                                return {
                                    ...ee,
                                    value: ee.patient_user_id,
                                    label:
                                        ee.user_name +
                                        ' ' +
                                        (ee.gender === '0'
                                            ? '未知'
                                            : ee.gender === '1'
                                                ? '男'
                                        : '女') +
                                        ' ' +
                                        ee.age +
                                        (ee.age ? '岁' : '') +
                                        ' ' +
                                        ee.mobile
                                };
                            })
                        );
                    });
                });
            }
        }, 700),
        remoteMethod(query) {
            if(query !== '') {
                this.getDatas(query);
            }else {
                this.oldPatient = [];
            }
        },
        remoteMethods(query) {
            if(query !== '') {
                this.getDatass(query);
            }else {
                this.optionscity = [];
            }
        },
        getDatass: lodash.debounce(function(params) {
            if(this.optionscity.every(ee => {
                return ee.label !== params; 
            })) {
                this.loadingcity = true;
                patientindexsearchcity({
                    keywords: params
                }).then(res => {
                    this.$nextTick(() => {
                        this.loadingcity = false;
                        this.optionscity = Object.freeze(
                            res.list.map(ee => {
                                return {
                                    value: ee.value + ',' + params,
                                    label: ee.label
                                };
                            })
                        );
                    });
                });
            }
        }, 700),
        
    }
    
};
</script>

<style scoped>

    /deep/.ivu-modal-mask{
        z-index: 1000 !important;
    }
    /deep/ .ivu-modal-wrap{
        z-index: 1000 !important;
    }
        .headerImgs {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            overflow: hidden;
        }
    
        .ivu-weixin {
            color: #777;
            font-size: 12px;
        }
    
        .sky-wxid {
            color: #777;
            margin-top: 2px;
            font-size: 12px;
        }
    
        .sky-record {
            /* border: 1px solid #dcdcdc; */
            min-height: 600px;
        }
    
        .ivu-active {
            border: 1px solid #2d8cf0;
        }
    
        .ivu-active:hover {
            border: 1px solid #2d8cf0 !important;
        }
    
        .person {
            color: #999;
        }
    
        /* 时间轴 */
        .time {
            font-size: 14px;
            font-weight: bold;
        }
    
        .content {
            padding: 10px 0;
        }
    
        .consultant {
            color: #9ea7b4;
        }
    
        h4 span {
            margin-left: 5px;
        }
    
        .side-bg {
            background: #f2f2f2;
            padding: 24px !important;
        }
    
        .return-form {
            padding: 24px;
        }
    
        .title {
            font-weight: bold;
            font-size: 16px;
            padding: 24px 0 0 24px;
        }
    
        .sky-screen {
            margin-top: 16px;
        }
    
        .sky-s1 {
            font-size: 14px;
            margin: 0 16px;
            color: #666;
        }
    
        .sky-s2 {
            font-size: 12px;
            margin-right: 10px;
            color: #888;
        }
    
        .sky-note {
            padding: 5px 0;
        }
</style>